package com.gjj.abox.login;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gjj.abox.BaseActivity;
import com.gjj.abox.account.PlatformListActivity;
import com.gjj.abox.constant.Constants;
import com.gjj.abox.custom.MessageDialog;
import com.gjj.abox.database.R;
import com.gjj.abox.support.FileChooserActivity;
import com.gjj.abox.utils.AnimalUtils;
import com.gjj.abox.utils.BackupTask;
import com.gjj.abox.utils.ToastUtil;


public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText etName;
    private EditText etPwd;
    Button btnLogin;
    String name, pwd, pwd_db;
    TextView tvRegister, tvForgetPwd,backUp;
    private static Context ctx;
    private static final int REQUEST_CODE = 1991;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        ctx = this;
        backUp = (TextView) findViewById(R.id.tv_login_backup);
        etName = (EditText) findViewById(R.id.edit_login_name);
        etPwd = (EditText) findViewById(R.id.edit_login_pwd);
        tvRegister = (TextView) findViewById(R.id.tv_login_register);
        tvForgetPwd = (TextView) findViewById(R.id.tv_login_forget_pwd);
        btnLogin = (Button) findViewById(R.id.button_login);
        etName.setText(sharedpreferences.getString("u_name", ""));
        btnLogin.setOnClickListener(this);
        backUp.setOnClickListener(this);
        tvRegister.setOnClickListener(this);
        tvForgetPwd.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.button_login:
                login();
                break;
            case R.id.tv_login_backup:
                startActivityForResult(new Intent(ctx,FileChooserActivity.class),1991);
                break;
            case R.id.tv_login_register:
                intent = new Intent(ctx, RegisterFirstActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_login_forget_pwd:
                forgetPwd();
                break;
        }
    }

    private void login() {
        if (!checkInput()) {
            return;
        }
        pwd_db = boxDb.queryPwdByName(name);
        if (pwd.equals(pwd_db)) {
            Intent intent = new Intent(LoginActivity.this, PlatformListActivity.class);
            edit.putString(Constants.User_Name, name);
            edit.putString("u_pwd", pwd_db);
            edit.commit();
            startActivity(intent);
            finish();
        } else {
            ToastUtil.showShortToast(LoginActivity.this, "登录失败！");
        }
    }

    private void forgetPwd() {
        if (etName.getText().toString().trim().equals("")) {
            ToastUtil.showShortToast(LoginActivity.this, "请输入用户名");
        } else {
            if (boxDb.checkUserAccountIsExist(etName.getText().toString().trim())) {
                edit.putString("name", etName.getText().toString());
                edit.apply();
                Intent intent = new Intent(ctx, BackPasswordActivity.class);
                startActivity(intent);
            } else {
                ToastUtil.showShortToast(LoginActivity.this, "用户名不存在！");
            }
        }
    }

    private boolean checkInput() {
        name = etName.getText().toString().trim();
        pwd = etPwd.getText().toString().trim();
        if (name.equals("")) {
            AnimalUtils.shakeAnimal(etName);
            return false;
        } else if (pwd.equals("")) {
            AnimalUtils.shakeAnimal(etPwd);
            return false;
        }
        return true;
    }

    //数据恢复
    private void dataResume(String path,String fileName) {
        boxDb.checkUserAccountIsExist("");
        new BackupTask(this, path.substring(0,path.length()-fileName.length()),fileName.substring(0,fileName.length()-3)).execute("restroeDatabase");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case 1991:
                if(resultCode == RESULT_OK) {
                    String path = data.getStringExtra("file_chooser");
                    String fileName = data.getStringExtra("file_name");
                    dataResume(path, fileName);
                }
                break;
        }
    }
}
